package com.allan.wiki_project.service;

import com.allan.wiki_project.domain.Ebook;
import com.allan.wiki_project.domain.EbookExample;
import com.allan.wiki_project.mapper.EbookMapper;
import com.allan.wiki_project.req.EbookReq;
import com.allan.wiki_project.resp.EbookResp;
import com.allan.wiki_project.util.CopyUtil;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EbookService {

    @Resource
    private EbookMapper ebookMapper;

    public List<EbookResp> list(EbookReq req) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria(); //内部类
        if(!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        //先转成Ebook类然后再遍历得到EbookResp
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);
        
        List<EbookResp> respList = new ArrayList<>();
//        for (Ebook ebook : ebookList) {
////            EbookResp ebookResp = new EbookResp();
////            BeanUtils.copyProperties(ebook, ebookResp); //将ebook的值拷贝到ebookResp
//            //再赋值进去
//            //现在又CopyUtil包不用那么麻烦了
//            EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
//            respList.add(ebookResp);
//        }

        //列表复制
        List<EbookResp> list = CopyUtil.copyList(ebookList, EbookResp.class);
        return list;
    }

}
